package gen.desktop.components;

import gen.core.managers.ConnConfigManager;
import gen.core.models.ConnConfig;
import gen.core.utils.GenUtils;
import gen.desktop.Application;
import gen.desktop.MainFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 * 主菜单栏
 *  
 * @author nswish@gmail.com
 */
public class MainMenuBar extends JMenuBar {
	private static final long serialVersionUID = 1056951138254870739L;
	
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenu mnNewMenu_1;
	private JMenuItem menuItemCreate;
	private JMenuItem menuItemModify;
	private JMenuItem menuItemDelete;
	private JMenu menu;
	private JMenu menuSetCurConn;
	private ButtonGroup buttonGroup;

	public MainMenuBar(){
		init();
	}
	
	/**
	 * 初始化
	 */
	public void init(){
		//----- 文件 -----
		mnNewMenu = new JMenu("文件");
		this.add(mnNewMenu);

		mntmNewMenuItem = new JMenuItem("退出");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mntmNewMenuItem.setIcon(new ImageIcon(MainFrame.class.getResource("/gen/desktop/icons/size16x16/door_out.png")));
		mnNewMenu.add(mntmNewMenuItem);

		//----- 数据库连接 -----
		mnNewMenu_1 = new JMenu("数据库连接");
		this.add(mnNewMenu_1);
		
		menu = new JMenu("管理连接");
		menu.setIcon(new ImageIcon(MainFrame.class.getResource("/gen/desktop/icons/size16x16/database_connect.png")));
		mnNewMenu_1.add(menu);

		menuItemCreate = new JMenuItem("创建连接");
		menuItemCreate.setIcon(new ImageIcon(MainFrame.class.getResource("/gen/desktop/icons/size16x16/database_add.png")));
		menuItemCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConnConfigDialog aConnConfigDialog = new ConnConfigDialog();
				aConnConfigDialog.setTitle(menuItemCreate.getText());
				aConnConfigDialog.setIconImage(((ImageIcon)menuItemCreate.getIcon()).getImage());
				aConnConfigDialog.setNew(true);
				aConnConfigDialog.setModal(true);
				aConnConfigDialog.setVisible(true);
				loadConnConfigToMenu();
			}
		});
		menu.add(menuItemCreate);

		menuItemModify = new JMenuItem("编辑连接");
		menuItemModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConnConfigDialog aConnConfigDialog = new ConnConfigDialog();
				aConnConfigDialog.setTitle(menuItemModify.getText());
				aConnConfigDialog.setIconImage(((ImageIcon)menuItemModify.getIcon()).getImage());
				aConnConfigDialog.setNew(false);
				aConnConfigDialog.setModal(true);
				aConnConfigDialog.setVisible(true);
				loadConnConfigToMenu();
			}
		});
		menuItemModify.setIcon(new ImageIcon(MainFrame.class.getResource("/gen/desktop/icons/size16x16/database_edit.png")));
		menu.add(menuItemModify);
		
		menuItemDelete = new JMenuItem("删除连接");
		menuItemDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConnConfigDelDailog aConnConfigDelDialog = new ConnConfigDelDailog();
				aConnConfigDelDialog.setTitle(menuItemDelete.getText());
				aConnConfigDelDialog.setIconImage(((ImageIcon)menuItemDelete.getIcon()).getImage());
				aConnConfigDelDialog.setModal(true);
				aConnConfigDelDialog.setVisible(true);
				loadConnConfigToMenu();
			}
		});
		menuItemDelete.setIcon(new ImageIcon(MainFrame.class.getResource("/gen/desktop/icons/size16x16/database_delete.png")));
		menu.add(menuItemDelete);

		menuSetCurConn = new JMenu("当前连接");
		menuSetCurConn.setIcon(new ImageIcon(MainFrame.class.getResource("/gen/desktop/icons/size16x16/database_lightning.png")));
		mnNewMenu_1.add(menuSetCurConn);
	}

	/**
	 * 装载数据库连接配置到菜单
	 */
	public void loadConnConfigToMenu() {
		Connection conn = null;
		ConnConfigManager aConnConfigManager = new ConnConfigManager();
		ConnConfig curConnConfig = getCurConnConfig();

		try {
			conn = Application.getSqliteClient().createConnection();
			
			buttonGroup = new ButtonGroup();
			menuSetCurConn.removeAll();
			
			ConnConfig[] configArray = aConnConfigManager.list(conn, Application.getOwner(), "Main");
			for(ConnConfig config : configArray){
				ConnConfigRadioButtonMenuItem aConnConfigRadioButtonMenuItem = new ConnConfigRadioButtonMenuItem();
				aConnConfigRadioButtonMenuItem.setConnConfig(config);
				buttonGroup.add(aConnConfigRadioButtonMenuItem);
				menuSetCurConn.add(aConnConfigRadioButtonMenuItem);
			}
			
			setCurConnConfig(curConnConfig);
		} catch (Exception ex) {
			ex.printStackTrace();
			JOptionPane.showMessageDialog(null, ex.getMessage());
		} finally {
			GenUtils.closeConnection(conn);
		}
	}

	/**
	 * 得到当前的连接配置
	 */
	public ConnConfig getCurConnConfig() {
		for(int i=0; i<menuSetCurConn.getItemCount();i++){
			ConnConfigRadioButtonMenuItem aItem = (ConnConfigRadioButtonMenuItem)menuSetCurConn.getItem(i);
			if(aItem.isSelected())
				return aItem.getConnConfig();
		}
		
		return null;
	}

	/**
	 * 设置当前的连接配置
	 */
	public void setCurConnConfig(ConnConfig curConnConfig) {
		if(curConnConfig == null){
			return;
		}
		
		for(int i=0; i<menuSetCurConn.getItemCount();i++){
			ConnConfigRadioButtonMenuItem aItem = (ConnConfigRadioButtonMenuItem)menuSetCurConn.getItem(i);
			if(aItem.getConnConfig().getId().equals(curConnConfig.getId()))
				aItem.setSelected(true);
		}
	}
}
